import {defineStore} from "pinia/dist/pinia";

import {login as remoteUserLogin} from "@/api/sys/user";

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    user: {},
  }),
  getters: {
    isAuthenticated: (state) => {
      return !!state.token || !!sessionStorage.getItem('token')
    }
  },
  actions: {
    async login({username, password}) {
      const {code, data} = await remoteUserLogin({username, password})
      if (code === 0) {
        this.token = data.token
        this.user = data.user
        sessionStorage.setItem('token', data.token)
      }
    }
  },
})
